<?php
class ViSeminarAssignAccomodation_Controller extends AdminController {

    public $ASSIGN_ACCOMODATION_LIST_UUID = 'admin/viSeminarAssignAccomodation/assign/viRegistrationList';

    public function __construct() {
        parent::__construct('viRegistration');
        $this->setDefaultListOrder('id');
    }

    public function form() {
        // if ($_SERVER['REQUEST_METHOD'] == 'GET')

        $viSeminarId = $this->input->get('id');

        // Basic validation
        if (! valid::isId($viSeminarId)) {
            $this->redirectToList('error.invalidId');
        }

        $viSeminar = $this->dao->getById('vi_seminar', $viSeminarId);
        if (! $viSeminar) {
            $this->redirectToList('error.recordNotFound');
        }

        $this->assignAccomodationData($viSeminarId);

        $formData = array();
        foreach ($this->viewData['viRegistrationList'] as $viRegistration) {
            $formData['roomNumber_' . $viRegistration['id']] = $viRegistration['room_number'];
        }

        $this->viewData['formData'] = $formData;
        $this->viewData['viSeminar'] = $viSeminar;

        $this->renderSubview('/admin/viSeminar/viAssignAccomodation');
    }

    private function selectRegistrationList($viSeminarId, $presentOnly) {
        $params = array('vi_seminar_id' => $viSeminarId, 'without_acc' => 'bez_ubytovani');
        $query = 'SELECT reg.*, ' .
                'per.first_name, per.last_name, per.birth_date, per.email, per.idcard_number, per.street, per.city, per.postal_code, per.notes AS person_notes, per.gender, ' .
                'c.code AS country_code, c.name_native AS country_name, ' .
                'vat.name AS vi_accomodation_type_name ' .
                'FROM vi_registration reg ' .
                'LEFT OUTER JOIN person per ON per.id = reg.person_id ' .
                'LEFT OUTER JOIN country c ON per.country_code = c.code ' .
                'LEFT OUTER JOIN vi_accomodation_type vat ON vat.code = reg.vi_accomodation_type_code ' .
                'WHERE reg.vi_seminar_id = :vi_seminar_id ' .
                'AND vat.code != :without_acc ' .
                'AND reg.cancelled_at IS NULL ';
        if ($presentOnly) {
            $query .= 'AND reg.is_present = :present ';
            $params['present'] = sql::$TRUE;
        }
        $query = sql::orderBy($this->ASSIGN_ACCOMODATION_LIST_UUID, $query, 'last_name');
        return $this->dao->queryAll($query, $params);
    }

    private function assignAccomodationData($viSeminarId) {
        $this->viewData['viRegistrationList'] = $this->selectRegistrationList($viSeminarId, FALSE);
    }

    public function assign() {
        $formData = $this->postToDbValues();
        $this->viewData['formData'] = $formData;

        $viSeminarId = $formData['vi_seminar_id'];
        $viSeminar = $this->dao->getById('vi_seminar', $viSeminarId);
        if (! $viSeminar) {
            $this->redirectToList('error.recordNotFound');
        }

        $this->viewData['viSeminar'] = $viSeminar;

        if ($formData['orderBy']) {
            $this->assignAccomodationData($viSeminarId);
            foreach ($this->viewData['viRegistrationList'] as &$viRegistration) {
                $viRegistration['room_number'] = $formData['roomNumber_' . $viRegistration['id']];
            }
            $this->renderSubview('/admin/viSeminar/viAssignAccomodation');
        }
        else {
            foreach ($formData['regIdList'] as $viRegistrationId) {
                $roomNumber = $formData['roomNumber_' . $viRegistrationId];
                $viRegistration = $this->dao->getById('vi_registration', $viRegistrationId);
                $viRegistration['room_number'] = $roomNumber;
                $this->dao->update('vi_registration', $viRegistration);
            }
            $this->msg->info('viSeminar.accomodationWasSaved');
            $this->redirect('/admin/viSeminar/display?id=' . $viSeminarId);
        }
    }
}
